<?php

	define('HOST_SMTP', 'mail.canraid.it');
	define('USER_SMTP', 'canraid.it15703');
	define('PASSWORD_SMTP', 'C15047300c!');
	define('PORT_SMTP', 587 );
	define('FROM_MAIL', 'hub@canraid.it');
 
//this functin processes the server return codes and generates errors if needed
function server_parse($socket, $expected_response)
{
	
    $server_response = '';
    while (substr($server_response, 3, 1) != ' ')
    {
        $server_response = fgets($socket, 256);
    }
 
    if (!(substr($server_response, 0, 3) == $expected_response))
        return $expected_response-'-->'.$server_response.'; ';
    else 
  		return '';
}
 
 
//
// This function was originally a part of the phpBB Group forum software phpBB2 (http://www.phpbb.com).
// They deserve all the credit for writing it. I made small modifications for it to suit PunBB and it's coding standards.
// -------> This message is from punBB developer
//
function smtp_mail($to, $subject, $message, $headers = '')
{
    $recipients = explode(',', $to);
    
    $trace='';
    
    if (!($socket = fsockopen(HOST_SMTP, PORT_SMTP, $errno, $errstr, 15)))
        $trace .= "Could not connect to smtp host '$smtp_host' ($errno) ($errstr)";
 
    $trace .= server_parse($socket, '220');
 
    fwrite($socket, 'EHLO '.HOST_SMTP."\r\n");
    $trace .= server_parse($socket, '250');
 
    fwrite($socket, 'AUTH LOGIN'."\r\n");
    $trace .= server_parse($socket, '334');
 
    fwrite($socket, base64_encode(USER_SMTP)."\r\n");
    $trace .= server_parse($socket, '334');
 
    fwrite($socket, base64_encode(PASSWORD_SMTP)."\r\n");
    $trace .= server_parse($socket, '235');
    
    fwrite($socket, str_replace("??FROM_MAIL??", FROM_MAIL , 'MAIL FROM: <??FROM_MAIL??>')."\r\n");
    $trace .= server_parse($socket, '250');
 
    foreach ($recipients as $email)
    {
        fwrite($socket, 'RCPT TO: <'.$email.'>'."\r\n");
        $trace .= server_parse($socket, '250');
    }
 
    fwrite($socket, 'DATA'."\r\n");
    $trace .= server_parse($socket, '354');
 
    fwrite($socket, 'Subject: '.$subject."\r\n".'To: <'.implode('>, <', $recipients).'>'."\r\n".$headers."\r\n\r\n".$message."\r\n");
 
    fwrite($socket, '.'."\r\n");
    $trace .= server_parse($socket, '250');
 
    fwrite($socket, 'QUIT'."\r\n");
    fclose($socket);
 	
 	if (trim($trace) == '' ){
 		return true;	
 	}else{
 		return false;
 	}
}
 

?>